/// @Classname : HcFullTitle
/// @Author :
/// @Time : 2023-05-18 16:53
/// @Description : 占满屏幕的全屏标题

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:hc_ui/constants/hc_size.dart';
import 'package:hc_ui/utils/hc_string_util.dart';

import '../../constants/hc_color.dart';

class HcFullTitle extends StatelessWidget {
  //标题
  final String? title;

  //副标题
  final String? subtitle;

  //标题样式
  final TextStyle titleStyle;

  //副标题样式
  final TextStyle subtitleStyle;

  //是否展示划线
  final bool showLine;

  //是否展示点
  final bool showDot;

  //点的组件
  final Widget? dotWidget;

  //线的自定义组件
  final Widget? lineWidget;

  //边框
  final EdgeInsets padding;

  //背景颜色
  final Color color;

  //起始角度
  final double startAngle;

  //副标题是否首字母大写
  final bool subTitleCapitalize;

  const HcFullTitle(
      {super.key,
      this.title,
      this.subtitle,
      this.titleStyle = const TextStyle(
        color: Colors.black,
        fontSize: HcFont.f16,
        fontWeight: FontWeight.bold,
      ),
      this.subtitleStyle = const TextStyle(
          fontSize: HcFont.f14,
          color: HcColor.defaultSubtitleColor,
          fontWeight: FontWeight.normal),
      this.showLine = true,
      this.showDot = false,
      this.dotWidget,
      this.padding = const EdgeInsets.all(18),
      this.color = Colors.green,
      this.startAngle = 0,
      this.subTitleCapitalize = true,
      this.lineWidget});

  @override
  Widget build(BuildContext context) {
    String? _subtitle =
        (subTitleCapitalize ? HcStringUtil.capitalizeStr(subtitle) : subtitle);
    return Padding(
      padding: padding,
      child: Row(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          if (showLine) _buildLine(0),
          if (showDot) _buildDot(startAngle),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 18),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                if (title != null)
                  Text(title!, style: titleStyle.copyWith(letterSpacing: 2.5)),
                if (_subtitle != null && _subtitle.isNotEmpty)
                  Text(
                    _subtitle,
                    style: subtitleStyle.copyWith(
                        color: subtitleStyle.color?.withOpacity(0.6)),
                  ),
              ],
            ),
          ),
          if (showDot) _buildDot(startAngle + 180),
          if (showLine) _buildLine(180),
        ],
      ),
    );
  }

  Widget _buildLine(transform) {
    return Expanded(
        child: Transform.rotate(
      angle: pi / 180 * transform,
      child: lineWidget ??
          Container(
            height: 1,
            decoration: BoxDecoration(
              gradient: LinearGradient(
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
                // 10% of the width, so there are ten blinds.
                colors: [Colors.white, color],
                // whitish to gray
                tileMode:
                    TileMode.repeated, // repeats the gradient over the canvas
              ),
            ),
          ),
    ));
  }

  Widget _buildDot(transform) {
    return Transform.rotate(
      angle: pi / 180 * transform,
      child: Transform.rotate(
          angle: pi / 9,
          child: dotWidget ??
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Container(
                    width: 4,
                    height: 15,
                    margin: const EdgeInsets.only(left: 5),
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(4),
                        color: (color).withOpacity(0.3)),
                  ),
                  Container(
                    width: 4,
                    height: 20,
                    margin: const EdgeInsets.only(left: 5, bottom: 5),
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(4), color: color),
                  ),
                ],
              )),
    );
  }
}
